package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.db.BlackList;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.mapper.BlackListMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 推荐给登录用户的今日佳人
     *
     * @param loginUserId
     * @return
     */
    @Override
    public RecommendUser findTodayBest(Long loginUserId) {
        //条件toUserId=登录用户id
        Query query = new Query(Criteria.where("toUserId").is(loginUserId));
        //按分数降序取第一个
        query.with(Sort.by(Sort.Order.desc("score")));
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 通过登录用户id分页查询好友推荐
     *
     * @param loginUserId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPageBy2UserId(Long loginUserId, Long page, Long pageSize) {
        //1. 构建查询的条件
        Query query = new Query(Criteria.where("toUserId").is(loginUserId));
        //2. 统计总数
        long total = mongoTemplate.count(query, RecommendUser.class);
        //3. 判断 总数 > 0 ?
        List<RecommendUser> recommendUserList = new ArrayList<>();
        if(total > 0) {
            //4. 设置分页参数
            query.skip((page-1)*pageSize).limit(pageSize.intValue());
            //5. 查询，得到分页的结果集
            recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        }
        //6. 构建pageResult返回
        return PageResult.pageResult(page,pageSize,recommendUserList, total);
    }

    /**
     * 查询佳人与登录用户的缘分值
     * recommend_user
     * @param loginUserId
     * @param userId
     * @return
     */
    @Override
    public Double queryForScore(Long loginUserId, Long userId) {
        //1. 构建查询的条件, toUserId=登录用户, userId=佳人的id
        Query query = new Query(Criteria.where("toUserId").is(loginUserId)
                .and("userId").is(userId));
        //2. 查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if(null != recommendUser){
            return recommendUser.getScore();
        }
        return null;
    }

    /**
     * 桃花传音（喜欢）
     * @param loginId
     * @param userLike
     */
    @Override
    public void like(Long loginId, UserLike userLike) {
        Query query = new Query(Criteria.where("userId").is(loginId).and("likeUserId").is(userLike.getLikeUserId()));
        long count = mongoTemplate.count(query, UserLike.class);
        if (count==0){
            mongoTemplate.insert(userLike);
            Query query1 = new Query(Criteria.where("userId").is(userLike.getLikeUserId()).and("likeUserId").is(loginId));
            if (mongoTemplate.count(query1, UserLike.class)!=0) {
                Friend friend = new Friend();
                friend.setUserId(loginId);
                friend.setFriendId(userLike.getLikeUserId());
                friend.setCreated(System.currentTimeMillis());
                // 不存在则要添加
                mongoTemplate.insert(friend);
                System.out.println("成功");
            }
        }
    }

    /**
     * 桃花传音不喜欢(加入黑名单)
     * @param userId
     * @param loginId
     */
    @Override
    public void unlove(Long userId,Long loginId) {

        BlackList blackList = new BlackList();
        blackList.setBlackUserId(userId);
        blackList.setUserId(loginId);

     if (blackListMapper.equals(blackList)) {
         blackListMapper.insert(blackList);
     }
    }


}
